<?php
class Mincoding_VendorConnect_Block_Adminhtml_Contract_Edit_Tab_Product extends Mage_Adminhtml_Block_Catalog_Category_Tab_Product
{
    protected function _prepareColumns()
    {
         parent::_prepareColumns();
         /*$this->addColumn('name2', array(
            'header'    => Mage::helper('vendorconnect')->__('Vendor'),
            'sortable'  => true,
            'width'     => '60',
            'index'     => 'name'
        ));*/
    }
    protected function _getSelectedProducts()
    {
        $products = $this->getRequest()->getPost('selected_products');
        if (is_null($products)) {
            if (  $currentContract = Mage::registry('vendorconnect_contract') )
            {
                if ( $currentContract->getId() )
                {
                    $contractProductResource = Mage::getModel('vendorconnect/resource_contractproduct');
                    return $contractProductResource->getProductIdsSelected($currentContract->getId());
                }
            }
            //$products = $this->getCategory()->getProductsPosition();
            //return array_keys($products);
        }
        return $products;
    }
    
    public function getGridUrl()
    {
        $url = $this->getUrl('*/*/grid', array('_current'=>true));
        if (  $currentContract = Mage::registry('vendorconnect_contract') )
        {
            if ( $currentContract->getId() )
            {
                $url .= '?contract_id=' . $currentContract->getId(); 
                $url = str_replace('id/' . $currentContract->getId(), 'id/' . 
                    $this->getRequest()->getParam('category_id',Mage::helper('vendorconnect')->getDefaultCategoryIdFilter()),$url);
                
            }
        }
        return $url;
    }
    
    protected function _prepareCollection()
    {
        $joinType = 'left';
        if ($this->getCategory()->getId()) {
            $this->setDefaultFilter(array('in_category'=>1));
            $joinType = 'inner';
        }
        $collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('sku')
            ->addAttributeToSelect('price')
            ->addStoreFilter($this->getRequest()->getParam('store'))
            ->joinField('position',
                'catalog/category_product',
                'position',
                'product_id=entity_id',
                'category_id='.(int) $this->getRequest()->getParam('category_id', Mage::helper('vendorconnect')->getDefaultCategoryIdFilter()),
                $joinType);//left inner
        $this->setCollection($collection);
        //echo $collection->getSelect()->__toString();

        if ($this->getCategory()->getProductsReadonly()) {
            $productIds = $this->_getSelectedProducts();
            if (empty($productIds)) {
                $productIds = 0;
            }
            $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
        }

        return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
    }
}